import datasetService from '@/api/services/datasetService';
import Modals from '@/components/modals/Modals';
import { Input, message } from 'antd';
import { useState } from 'react';

interface Props {
  isModalOpen: boolean;
  setIsModalOpen: (value: boolean) => void;
  approvalData?: any;
  onSuccessful(): void;
}

export default function RejectReason({
  isModalOpen,
  setIsModalOpen,
  onSuccessful,
  approvalData,
}: Props) {
  const [rejectValue, setRejectValue] = useState('');
  const [messageApi, contextHolder] = message.useMessage();
  // 发起重新审批
  const reApproval = async () => {
    let ids: any = [approvalData._id];
    try {
      await datasetService.reviewApprovalDocument({
        ids,
        operator: approvalData.reviewStatus == 'upload_reject' ? 'upload' : 'delete',
        reason: rejectValue,
        appId: approvalData.appId,
        reviewStatus:
          approvalData.reviewStatus == 'upload_reject'
            ? 'upload_reject_review'
            : 'delete_reject_review',
      });
      messageApi.success('重新审批发布成功');
      setIsModalOpen(false);
      setRejectValue(''); //把驳回原因清空
      onSuccessful && onSuccessful();
    } catch (error) {
      messageApi.error('审核失败');
    }
  };

  return (
    <div>
      {contextHolder}
      <Modals
        title={<div className="text-nowrap text-sm text-[#474C5F]">重新审批</div>}
        zIndex={1000}
        open={isModalOpen}
        onOk={
          reApproval //重新审批
        }
        onCancel={() => {
          setIsModalOpen(false);
          setRejectValue(''); //把驳回原因清空
        }}
      >
        <Input.TextArea
          placeholder={'请输入重新审批原因'}
          autoSize={{ minRows: 10, maxRows: 10 }}
          classNames={{
            textarea: '!border-0 !bg-[#F3F4F9]',
          }}
          value={rejectValue}
          onChange={(e) => {
            setRejectValue(e.target.value);
          }}
        />
      </Modals>
    </div>
  );
}
